﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.抖音出入库.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库.抖音下单
{
    public partial class Form秒杀发货详情 : DevExpress.XtraEditors.XtraForm
    {
        private List<RetailUserOrderDTO> retailUserOrders未发货 = new List<RetailUserOrderDTO>();
        private List<RetailUserOrderDTO> retailUserOrders待发货 = new List<RetailUserOrderDTO>();
        private List<RetailUserOrderDTO> retailUserOrders已发货 = new List<RetailUserOrderDTO>();
        private String mobile;
        public Form秒杀发货详情(String mobile)
        {
            InitializeComponent();
            this.mobile = mobile;
            this.区分已发货和未发货(mobile);
            this.最新地址(mobile);
        }

        private void 最新地址(String mobile)
        {
            try
            {
                DouyinCustomerAddressDao douyinCustomerAddress = 抖音Service.getLatestCustomerAddressLog(mobile);
                this.textBox客户.Text = douyinCustomerAddress.customerName;
                this.textBox电话.Text = douyinCustomerAddress.mobile;
                this.textBox地址.Text = douyinCustomerAddress.formattedAddress;
                this.textBox门牌号.Text = douyinCustomerAddress.addressNumber;
                this.textBox定位级别.Text = douyinCustomerAddress.level;
                //  定位图
                this.pictureBox定位图.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + douyinCustomerAddress.location + "&zoom=10&size=750*300&markers=mid,,A:" + douyinCustomerAddress.location + "&key=1e35c2196190d3f06060fb90f1c4ce30");
            }
            catch { }
        }

        private void 区分已发货和未发货(String mobile)
        {
            //  查询客户所有订单
            List<RetailUserOrderDTO> retailUserOrders = 零售Service.getRetailUserOrderByMobile(mobile,"SSHIRT","18");
            foreach (RetailUserOrderDTO item in retailUserOrders)
            {
                if (item.ifDelivery)
                {
                    //  已发货
                    this.retailUserOrders已发货.Add(item);
                }
                else
                {
                    //  未发货
                    this.retailUserOrders未发货.Add(item);
                }
            }
            //  清空待发货
            this.retailUserOrders待发货.Clear();
            //  绑定控件
            this.gridControl未发货.DataSource = this.retailUserOrders未发货;
            this.gridView未发货.RefreshData();
            this.gridControl已发货.DataSource = this.retailUserOrders已发货;
            this.gridView已发货.RefreshData();
            this.gridControl待发货.DataSource = this.retailUserOrders待发货;
            this.gridView待发货.RefreshData();
        }

        private void gridView未发货_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (this.retailUserOrders未发货.Count>0)
            {
                String id = this.gridView未发货.GetRowCellValue(this.gridView未发货.FocusedRowHandle, "id").ToString();
                for (int i = this.retailUserOrders未发货.Count - 1; i >= 0; i--)
                {
                    if (id.Equals(this.retailUserOrders未发货[i].id))
                    {
                        //  未发货进入待发货
                        this.retailUserOrders待发货.Add(this.retailUserOrders未发货[i]);
                        this.retailUserOrders未发货.Remove(this.retailUserOrders未发货[i]);
                        break;
                    }
                }
                //  刷新
                this.gridView未发货.RefreshData();
                this.gridView待发货.RefreshData();
            }
        }

        private void gridView待发货_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (this.retailUserOrders待发货.Count > 0)
            {
                String id = this.gridView待发货.GetRowCellValue(this.gridView待发货.FocusedRowHandle, "id").ToString();
                for (int i = this.retailUserOrders待发货.Count - 1; i >= 0; i--)
                {
                    if (id.Equals(this.retailUserOrders待发货[i].id))
                    {
                        //  待发货进入未发货
                        this.retailUserOrders未发货.Add(this.retailUserOrders待发货[i]);
                        this.retailUserOrders待发货.Remove(this.retailUserOrders待发货[i]);
                        break;
                    }
                }
                //  刷新
                this.gridView未发货.RefreshData();
                this.gridView待发货.RefreshData();
            }
        }

        private void simpleButton发货_Click(object sender, EventArgs e)
        {
            if (this.retailUserOrders待发货.Count<=0)
            {
                MessageBox.Show("无待发货订单");
                return;
            }
            List<String> ids = new List<String>();
            //  循环取id
            foreach (RetailUserOrderDTO item in this.retailUserOrders待发货)
            {
                ids.Add(item.id);
            }
            //  更新订单为已发货
            零售Service.setRetailOrderDelivery(ids);
            // 重新查询
            this.区分已发货和未发货(this.mobile);
        }
    }
}